package hczc.longcai.com.hccrowdfunding.home.fourth_mine.fragments;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.application.BaseApplication;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.beans.Address;

/**
 * A simple {@link Fragment} subclass.
 */
public class EditAddressFragment extends Fragment implements View.OnClickListener {

    private boolean isDefault = true;

    private Bundle arguments;
    private Address info;
    private Address c_info;

    private EditText t_name;
    private EditText t_phone;
    private TextView t_area;
    private EditText t_address;
    private RelativeLayout moren;
    private View moren_enter;

    public int id;
    private int index;
    private String name;
    private String area;
    private String address;
    private String phone;


    public EditAddressFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initDatas();
    }

    private void initDatas() {
        arguments = getArguments();
        info = ((Address) arguments.getSerializable("address"));
        getAddress();

    }

    private void getAddress() {
        id = info.getId();
        isDefault = info.isDfault();
        index = info.getIndex();
        name = info.getName();
        phone = info.getPhone();
        area = info.getArea();
        address = info.getAddress();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_edit_address, container, false);
        BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));

        initViews(view);
        setDatas();

        return view;
    }

    private void setDatas() {
        t_name.setHint(name);
        t_phone.setHint(phone);
        t_area.setText(area);
        t_address.setHint(address);
        if (isDefault) {
            moren_enter.setSelected(true);
        }
        moren.setOnClickListener(this);
    }


    private void initViews(View view) {

        t_name = ((EditText) view.findViewById(R.id.edit_address_information_name));
        t_phone = ((EditText) view.findViewById(R.id.edit_address_information_phone));
        t_area = ((TextView) view.findViewById(R.id.edit_address_information_area));
        t_address = ((EditText) view.findViewById(R.id.edit_address_information_address));

        moren = ((RelativeLayout) view.findViewById(R.id.edit_address_moren));
        moren_enter = view.findViewById(R.id.edit_address_moren_pic);

    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.edit_address_moren:
                if (isDefault) {
                    moren_enter.setSelected(true);
                    isDefault = false;
                } else {
                    moren_enter.setSelected(false);
                    isDefault = true;
                }
                break;
        }
    }

    public void updateAddress() {
        if (getDatas()) {
            BaseApplication.dbAddressController.updateAddress(c_info);
            getActivity().setResult(1);
            getActivity().finish();
        }
    }

    private boolean getDatas() {

        boolean flag = true;

        c_info = new Address();

        String e_name = t_name.getText().toString().trim();
        String e_phone = t_phone.getText().toString().trim();
        String e_address = t_address.getText().toString().trim();
        String e_area = "黑龙江省哈尔滨市";
        boolean e_default = moren_enter.isSelected();


        if ("".equals(e_name)) {
            e_name = name;
        }
        if ("".equals(e_phone)) {
            e_phone = phone;
        }
        if ("".equals(e_address)) {
            e_address = address;
        }

        c_info.setId(id);
        c_info.setIndex(index);
        c_info.setName(e_name);
        c_info.setPhone(e_phone);
        c_info.setArea(e_area);
        c_info.setAddress(e_address);
        c_info.setDfault(e_default);

        return flag;
    }
}
